class BusinessCard {
  final int? id;
  final String name;
  final String company;
  final String position;
  final String phone;
  final String email;
  final String address;
  final String website;
  final String? imagePath;
  final DateTime createdAt;
  final DateTime updatedAt;
  final String? notes;

  BusinessCard({
    this.id,
    required this.name,
    required this.company,
    required this.position,
    required this.phone,
    required this.email,
    required this.address,
    required this.website,
    this.imagePath,
    required this.createdAt,
    required this.updatedAt,
    this.notes,
  });

  // 从数据库创建对象
  factory BusinessCard.fromMap(Map<String, dynamic> map) {
    return BusinessCard(
      id: map['id'],
      name: map['name'] ?? '',
      company: map['company'] ?? '',
      position: map['position'] ?? '',
      phone: map['phone'] ?? '',
      email: map['email'] ?? '',
      address: map['address'] ?? '',
      website: map['website'] ?? '',
      imagePath: map['image_path'],
      createdAt: DateTime.parse(map['created_at']),
      updatedAt: DateTime.parse(map['updated_at']),
      notes: map['notes'],
    );
  }

  // 转换为数据库格式
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'company': company,
      'position': position,
      'phone': phone,
      'email': email,
      'address': address,
      'website': website,
      'image_path': imagePath,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'notes': notes,
    };
  }

  // 复制对象并修改部分属性
  BusinessCard copyWith({
    int? id,
    String? name,
    String? company,
    String? position,
    String? phone,
    String? email,
    String? address,
    String? website,
    String? imagePath,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? notes,
  }) {
    return BusinessCard(
      id: id ?? this.id,
      name: name ?? this.name,
      company: company ?? this.company,
      position: position ?? this.position,
      phone: phone ?? this.phone,
      email: email ?? this.email,
      address: address ?? this.address,
      website: website ?? this.website,
      imagePath: imagePath ?? this.imagePath,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      notes: notes ?? this.notes,
    );
  }

  // 检查名片是否完整
  bool get isComplete {
    return name.isNotEmpty && 
           company.isNotEmpty && 
           position.isNotEmpty && 
           phone.isNotEmpty && 
           email.isNotEmpty;
  }

  // 获取显示名称
  String get displayName {
    return name.isNotEmpty ? name : '未命名名片';
  }

  // 获取公司职位信息
  String get companyPosition {
    if (company.isNotEmpty && position.isNotEmpty) {
      return '$company - $position';
    } else if (company.isNotEmpty) {
      return company;
    } else if (position.isNotEmpty) {
      return position;
    }
    return '';
  }

  @override
  String toString() {
    return 'BusinessCard{id: $id, name: $name, company: $company, position: $position}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is BusinessCard && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
